/**
 * 展示所有动态列表
 */
app.controller('reflection', function ($scope, $http) {
    var token = localStorage.getItem('token');
    $scope.dynamicInfos = [];
    $scope.likeList = [];
    $scope.page = '';
    $scope.page = 1;

    $scope.showAllDynamic = function (page) {
        $scope.page = page ? page : 1;

        // 若该加载为初始化加载，即没有传入page参数
        if (!page) {
            $scope.page = 1;
        }
        $http({
            method: 'get',
            url: app.netPath + '/dynamic/showAllByPage',
            params: {'page': $scope.page}
        }).then(function (res) {
            console.log(res.data);
            //查询无错
            if (!res.data.error) {
                //登录
                if (localStorage.getItem('token')) {
                    $http({
                        method: 'get',
                        url: app.netPath + '/dynamic/show/likes',
                        headers: {Authorization: localStorage.getItem('token')}
                    }).then(function (res1) {
                        if (!res1.data.error) {
                            $scope.likeList = res1.data.data;
                            console.log(res1.data);
                            for (var i = 0; i < res.data.data.length; i++) {
                                //放置用户头像
                                if (res.data.data[i].user.userAvatarAddress) {
                                    res.data.data[i].user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user.userAvatarAddress;
                                    app.buildImageUrl(res.data.data[i].user, 'userAvatarAddress', function () {
                                        $scope.$digest();
                                    });
                                }
                                //放置动态图片
                                if (res.data.data[i].dynamicImg) {
                                    //得到每条动态的图片数组
                                    var dynamicImgs = $scope.getDynamicImg(res.data.data[i].dynamicImg);
                                    for (var j = 0; j < dynamicImgs.length; j++) {
                                        dynamicImgs[j] = app.netPath + '/resources/dynamicImg/' + dynamicImgs[j];
                                        app.buildImageUrl(dynamicImgs, j, function () {
                                            $scope.$digest();
                                        });
                                    }
                                    res.data.data[i].dynamicImg = dynamicImgs;

                                }
                                for (var index = 0; index < $scope.likeList.length; index++) {
                                    if (res.data.data[i].dynamicId == $scope.likeList[index].dynamicId) {
                                        res.data.data[i].isLike = true;
                                    } else {

                                    }
                                }

                                //将动态逐条存入（dynamicInfos）数组
                                $scope.dynamicInfos.push(res.data.data[i]);
                            }

                        }
                        $scope.isOnloading = false;
                        console.log($scope.dynamicInfos);


                        //当前页
                        $scope.thispage = $scope.dynamicInfos.length % 5 == 0 ? $scope.dynamicInfos.length / 5 : $scope.dynamicInfos.length / 5 + 1;
                        $scope.thispage = parseInt($scope.thispage);
                        var infiniteScroll = $('.infinite-scroll');
                        var infiniteScrollPreloader = $('.infinite-scroll-preloader');
                        // 启用无限加载事件
                        $.attachInfiniteScroll(infiniteScroll);


                        console.log($scope.thispage);
                        if (res.data.allPage <= $scope.thispage) {
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            $.detachInfiniteScroll(infiniteScroll);
                            // 删除加载提示符
                            infiniteScrollPreloader.hide();
                        }

                    }, function () {
                        $.toast('网络走丢了！')
                    });
                } else {
                    //没有登录
                    for (var i = 0; i < res.data.data.length; i++) {
                        //放置用户头像
                        if (res.data.data[i].user.userAvatarAddress) {
                            res.data.data[i].user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user.userAvatarAddress;
                        }
                        //放置动态图片
                        if (res.data.data[i].dynamicImg) {
                            //得到每条动态的图片数组
                            var dynamicImgs = $scope.getDynamicImg(res.data.data[i].dynamicImg);
                            for (var j = 0; j < dynamicImgs.length; j++) {
                                dynamicImgs[j] = app.netPath + '/resources/dynamicImg/' + dynamicImgs[j];

                            }
                            res.data.data[i].dynamicImg = dynamicImgs;
                        }
                        //将动态逐条存入（dynamicInfos）数组
                        $scope.dynamicInfos.push(res.data.data[i]);
                    }
                    $scope.isOnloading = false;
                    //当前页
                    $scope.thispage = $scope.dynamicInfos.length % 5 == 0 ? $scope.dynamicInfos.length / 5 : $scope.dynamicInfos.length / 5 + 1;
                    $scope.thispage = parseInt($scope.thispage);
                    var infiniteScroll = $('.infinite-scroll');
                    var infiniteScrollPreloader = $('.infinite-scroll-preloader');
                    // 启用无限加载事件
                    $.attachInfiniteScroll(infiniteScroll);
                    // 启用提示符
                    infiniteScrollPreloader.show();
                    if (res.data.allPage <= $scope.thispage) {
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        $.detachInfiniteScroll(infiniteScroll);
                        // 删除加载提示符
                        infiniteScrollPreloader.hide();
                    }


                }


            }

        })


    };
    //点赞
    $scope.addLike = function (dynamicId, index) {

        if (token) {
            $http({
                method: 'get',
                url: app.netPath + '/dynamic/giveLike',
                headers: {Authorization: token},
                params: {dynamicId: dynamicId}
            }).then(function (res) {
                if (!res.data.error) {
                    $.toast(res.data.msg);
                    $scope.dynamicInfos[index].isLike = true;

                    // location.href = '#reflection'
                } else {
                    $.toast("你已经顶过了");
                    $scope.dynamicInfos[index].isLike = true;
                }
            });
        } else {
            $scope.dynamicInfos[index].isLike = false;
            $.confirm('您还未登录，马上去登陆', function () {
                location.href = '#user-login';
            });
        }

    };

    //动态图片分割（","）
    $scope.getDynamicImg = function (dynamicImgString) {
        var dynamicImg = dynamicImgString.split(',');
        return dynamicImg;
    };
    //跳转到动态详情，得到一个dynamicId

    $scope.getDynamicId = function (dynamicId, isLike) {
        if (null == isLike) {
            isLike = 0;
        } else {
            isLike = 1;
        }
        sessionStorage.setItem('isLike', isLike);
        sessionStorage.setItem('dynamicId', dynamicId);
        location.href = '#evaluate-details'
    }
});